# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        def compare(left, right):
            if not left and not right: return True
            if not left or not right: return False
            if left.val != right.val: return False
            # print(left.val, right.val)
            return compare(left.left, right.right) and compare(left.right, right.left)
        return compare(root.left, root.right) if root else True

if __name__ == '__main__':
    root = TreeNode(1)
    left1, right1 = TreeNode(2), TreeNode(2)
    left21, right21, left22, right22 = TreeNode(3), TreeNode(4), TreeNode(4), TreeNode(3)
    root.left, root.right = left1, right1
    left1.left, left1.right = left21, right21
    right1.left, right1.right = left22, right22
    s = Solution()
    result = s.isSymmetric(root)
    print(result)
